﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CE.User;
using CE.Global;
using CE.Provider;

public partial class Provider_PersonalAccident_ProviderPAOccupation : System.Web.UI.Page
{
    string PageName = "CXPA";
    string SubPageName = "CXPAOCP";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        CheckMainPageAccess();
        CheckSubPageAccess();

        if (!IsPostBack)
        {
            Session[General.ViewedDetailsId] = LogUserViewedHistory().ToString();
        }
    }

    protected void ddlOccupationClass_SelectedIndexChanged(object sender, EventArgs e)
    {
        cblOccupation.Items.Clear();
        if (ddlOccupationClass.SelectedIndex > 0)
        {
            LoadAllOccupations(Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()), Convert.ToInt16(ddlOccupationClass.SelectedValue));
        }
    }

    protected void SelectAllOccupations(object sender, EventArgs e)
    {
        if (lnkbtnSelectAll.Text == "Select all Occupations")
        {
            for (int i = 0; i < cblOccupation.Items.Count; i++)
            {
                cblOccupation.Items[i].Selected = true;
            }

            lnkbtnSelectAll.Text = "Unselect all Occupations";
        }
        else
        {
            for (int i = 1; i < cblOccupation.Items.Count; i++)
            {
                cblOccupation.Items[i].Selected = false;
            }
            lnkbtnSelectAll.Text = "Select all Occupations";
        }



    }

    protected void LoadAllOccupations(Int64 ProviderCompanyId, Int16 ClassId)
    {
        OccupationsDB _occupationsdb = new OccupationsDB();
        DataTable _dtoccupations = _occupationsdb.GetOccupationsList(ProviderCompanyId, ClassId);
        if (_dtoccupations.Rows.Count > 0)
        {
            pnlOccupation.Visible = true;
            cblOccupation.DataSource = _dtoccupations;
            cblOccupation.DataTextField = "Description";
            cblOccupation.DataValueField = "OccupationId";
            cblOccupation.DataBind();

            cblOccupation.RepeatColumns = 4;
            cblOccupation.RepeatDirection = RepeatDirection.Horizontal;

            for (int i = 0; i < cblOccupation.Items.Count; i++)
            {
                foreach (DataRow row in _dtoccupations.Rows)
                {
                    //if (row["Status"].ToString())
                    if (row["Status"].ToString() == "True" && cblOccupation.Items[i].Value == row["OccupationId"].ToString())
                        cblOccupation.Items[i].Selected = true;
                }
            }
        }
        else
        {
            pnlOccupation.Visible = false;
        }
    }

    protected void ibtSubmit_Click(object sender, ImageClickEventArgs e)
    {
        if (Page.IsValid)
        {
            Occupations _occupations = new Occupations();
            OccupationsDB _occupationsdb = new OccupationsDB();

            Int64 newacid = 0;
            for (int i = 0; i < cblOccupation.Items.Count; i++)
            {
                //if (cblOccupation.Items[i].Selected == true)
                //{
                    _occupations.ProviderCompanyId = Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString());
                    _occupations.CXOccupationId = Convert.ToInt32(cblOccupation.Items[i].Value.ToString());
                    _occupations.Description = cblOccupation.Items[i].Text.ToString();
                    _occupations.Class = Convert.ToInt16(ddlOccupationClass.SelectedValue);
                    _occupations.Status = cblOccupation.Items[i].Selected;
                    _occupations.RefCEUserId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());
                    _occupations.ViewedDetailId = Convert.ToInt64(Session[General.ViewedDetailsId].ToString());
                    _occupationsdb.Insert(_occupations);

                    newacid = _occupations.ProviderPAOccupationId;
                //}

            }
            lnkbtnSelectAll.Text = "select all countries";
        }
    }

    private Int64 LogUserViewedHistory()
    {
        UserViewHistoryDB _userviewdb = new UserViewHistoryDB();
        return _userviewdb.LogViewedHistory("PA", Request, "Provider PA Add/Edit Occupations form.");
    }

    protected void ibtBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("~/Provider/PersonalAccident/PAInsuranceHome.aspx");
    }

    private void CheckMainPageAccess()
    {
        Int64 PageAccess = General.GetProviderMainPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), PageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
    }

    private void CheckSubPageAccess()
    {
        Int64 PageAccess = General.GetProviderSubPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), SubPageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
        else if (PageAccess == 100)
        {
            ibtSubmit.Enabled = false;
        }
    }

}
